package cn.edu.cuit.elena.web.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImportMonitorServlet
    extends HttpServlet
{

    static final String IMPORTING_CONSOLE = "importconsole";

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public ImportMonitorServlet()
    {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy()
    {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
    {

        doPost( request, response );
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
    {

        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();
        String para = request.getParameter( "kind" );
        String username = request.getParameter( "username" );
        String password = request.getParameter( "password" );
        String url = request.getParameter( "url" );
        String driver = request.getParameter( "driver" );
        String tablename = request.getParameter( "tablename" );
        //TODO task progress
        //
        //        String taskName = request.getParameter( "taskname" );
        //
        //        int progress = InitializationServlet.taskMonitor.getTaskProgresson( taskName );
        //
        //        String json = JsonHelper.getNewInstance().toJson( progress );
        //
        //        out.write( json );
        //        out.flush();
        //        out.close();

        if( IMPORTING_CONSOLE.equals( para ) )
        {
            String taskId = url + username + password + driver + tablename;
            String[] lines = cn.edu.cuit.elena.web.System.getSystemTaskMonitor().getTaskOutput( taskId );
            if( lines == null || lines.length == 0 )
            {
                try
                {
                    lines = cn.edu.cuit.elena.web.System.getSystemTaskMonitor().getRetiredTaskById( taskId )
                        .getOutput();
                }
                catch( NullPointerException e )
                {
                    out.close();
                    return;
                }
            }
            String[] tempArray;
            StringBuilder stringBuilder;
//
//            if( lines != null && lines.length > 15 )
//            {
//                tempArray = new String[15];
//                for( int index = 0, index_1 = 15; index < 15; index++, index_1-- )
//                {
//                    tempArray[index] = lines[lines.length - index_1 - 1];
//                }
//                lines = tempArray;
//            }
            try
            {

                stringBuilder = new StringBuilder();

                for( String line : lines )
                {
                    if( line.trim().length() == 0 )
                    {
                        continue;
                    }
                    stringBuilder.append( line );
                    stringBuilder.append( "\n" );
                }
            }
            catch( NullPointerException e )
            {
                out.close();
                return;
            }

            out.write( new String( stringBuilder ) );
            out.flush();
            out.close();
        }
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException
    {

    }

}
